#include "stdafx.h"
#include "SQLServer.h"

SQLServer::SQLServer():bHave(0),bSave(0), iFieldCount(0),iRecordCount(0),bLink (0)
{

}

bool	SQLServer::Link( string uid , string pwd , string  IP , string Database , string Provider )
{
	string p ("Provider=") ;
	p += Provider ;
	p += string(";Server="); 
	p += IP;
	p += string(";Database=");
	p += Database; 
	p += string("; uid=") ;
	p += uid ;
	p += string("; pwd=");
	p += pwd ;
	p += string(";");

	return this->Link (p) ;
}

bool	SQLServer::Link(std::string connect_str) 
{
	if ( !this->bLink )
	{
		::CoInitialize(NULL);
		m_pConnection.CreateInstance("ADODB.Connection");
		try
		{
			m_pConnection->Open( (_bstr_t) connect_str.c_str() , "" , "" ,adModeUnknown );
		}
		catch( ... )
		{
			return false ;
		}
		m_pRecordset.CreateInstance ("ADODB.Recordset");
		m_pRecordset->LockType = adLockOptimistic ;
		m_pRecordset->CursorType = adOpenStatic;
		m_pRecordset->CursorLocation = adUseClient;
		this->bLink = true ;
	}
	return true ;
}

SQLServer::~SQLServer( void )
{
	if ( this->bLink )
	{
		if ( m_pRecordset->State )
			m_pRecordset->Close();
		if ( m_pConnection->State )
			m_pConnection -> Close();
		::CoUninitialize();
	}
}

bool	SQLServer::operator << ( string cmd )
{
	try
	{
		m_pRecordset = m_pConnection->Execute((char*)cmd.c_str() , NULL , 1 );
	}
	catch (...)
	{
		return false ;
	}
	return true ;
}

int	SQLServer::RecordCount ()
{
	iRecordCount = 0 ;
	try
	{
		m_pRecordset->MoveFirst();
	}
	catch (  _com_error e )
	{
		this->iRecordCount = 0 ;
	}
	bHave = 1 ;
	while ( !m_pRecordset->EndOfFile && ++iRecordCount)
		m_pRecordset->MoveNext();
	iFieldCount = m_pRecordset->GetFields()->Count;

	return iRecordCount ;
}

bool	SQLServer::MoveFirst()
{
	try
	{
		m_pRecordset->MoveFirst();
	}
	catch (  _com_error e )
	{
		return false ;
	}
	return true ;
}

bool	SQLServer::MoveNext()
{
	m_pRecordset->MoveNext () ;
	if ( m_pRecordset->EndOfFile )
		return false ;
	return true ;
}

string SQLServer::GetFieldsName(int nField )
{
	_variant_t vt((long)nField);
	return string (m_pRecordset->GetFields()->Item[vt]->Name);
}

int SQLServer::In( int RecordNum , string FieldName , _variant_t index )
{
	if ( RecordNum < iRecordCount && RecordNum >= 0 )
	{	
		MoveTo(RecordNum);
		//m_pRecordset->Move( RecordNum );
		/*m_pRecordset->MovePrevious();*/
		m_pRecordset->PutCollect ( FieldName.c_str () , index );
		bSave = 1 ;
		return 0;
	}
	return -1;
}

_variant_t	SQLServer::Out ( string FieldName )
{
	_variant_t	send = m_pRecordset->GetCollect ( FieldName.c_str ());
	if ( send.vt == VT_NULL )
		send.Clear();
	return send;	
}

_variant_t SQLServer::Out ( long RecordNum , string FieldName )
{
	_variant_t send;
	
	if ( RecordNum < iRecordCount && RecordNum >= 0 )
	{
		MoveTo(RecordNum);
		send = m_pRecordset->GetCollect ( FieldName.c_str ());
		if ( send.vt == VT_NULL )
			send.Clear();
		return send;
	}
	throw "OVER!";
}

void SQLServer::Save()
{
	if ( bSave )
	{
		m_pRecordset->Update ();
		bSave = 0;
	}
}

void SQLServer::MoveTo ( long Add )
{	
	if ( Add >= 0 || Add < iRecordCount )
	{
		m_pRecordset->MoveFirst();
		m_pRecordset->Move( Add );
	}
}

_variant_t SQLServer::Out ( long RecordNum , long FieldNum )
{
	_variant_t send;
	
	if ( RecordNum < iRecordCount && RecordNum >= 0 )
	{
		MoveTo(RecordNum);
		send = m_pRecordset->GetCollect( (_variant_t)FieldNum );
		if ( send.vt == VT_NULL )
			send.Clear();
		return send;
	}
	throw "OVER!";
}